package com.shuqi.model;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.view.WindowManager;

import com.shuqi.bookcontent.BookContentRender.IBookContentRenderSetting;
import com.shuqi.common.Constant;
import com.shuqi.common.PayTheme;
import com.shuqi.common.utils.Log4an;
import com.shuqi.common.utils.SharedPreferenceUtil;
import com.shuqi.common.utils.Util;
import com.shuqi.controller.R;

/**
 * 
 * 阅读页设置
 * 
 * @remark 包含了所有关于阅读页设置的 方法和属性，可以看做是一个 设置的工具类。 提供单例的mSettings对象以供外部调用
 * 
 * @author Jianda.Yang
 * @date 2013-12-19 下午8:52:59
 * @version 1.0
 */
public class BookContentSettings implements IBookContentRenderSetting {
    public static final int TURNMODE_BOOK = 0;
    public static final int TURNMODE_Push = 0;
    public static final int TURNMODE_Scroll = 0;
    // =================by lyz
    /** 字档 0-9 */
    private int mSizePosition;

    /** 风格 0-3 */
    public int mStyle;

    /** 正文字号 */
    private int mTextSize;
    public static int BOOKCONTENT_DEFAULT_TEXT_SIZE;// 默认正文字号大小
    public static int BOOKCONTENT_TEXT_SIZE_CHANGE;// 字号改变梯度
    public static int BOOKCONTENT_TEXT_SIZE_MAX;// 字体最大
    public static int BOOKCONTENT_TEXT_SIZE_MIN;// 字体最小

    /** 标题字号 */
    private int mTitleTextSize;
    public static int TITLE_DEFAULT_TEXT_SIZE;// 默认标题大小
    public static int TITLE_TEXT_SIZE_CHANGE;// 标题字号改变梯度
    public static int TITLE_TEXT_SIZE_MIN;// 标题字号最小

    /** 标题与正文间距 */
    private int mTitleSpace;// 标题与正文间距
    public static int TITLE_DEFAULT_SPACE;// 标题与正文默认间距
    public static int TITLE_SPACE_CHANGE;// 标题与正文间梯度
    public static int[] TITLE_SPACES_MIN = new int[4];// 四种风格分别得最小间距

    /** 行间距 */
    private int mLineSpace;
    public static int LINE_DEFAULT_SPACE;// 默认行间距
    public static int LINE_SPACE_CHANGE;// 行间距梯度
    public static int[] LINE_SPACES_MIN = new int[4];// 四种风格分别得最小行间距

    /** 段间距 */
    private int mParagraphSpace;
    public static int PARAGRAPH_DEFAULT_SPACE;// 默认段间距
    public static int PARAGRAPH_DEFAULT_CHANGE;// 段间距梯度
    public static int[] PARAGRAPH_SPACES_MIN = new int[4];// 四种风格分别得最小段间距

    // ================
    /** 是否是全屏模式 */
    private boolean mIsFullScreen;
    /** 是否是竖屏 */
    private boolean mIsVertialScreen;
    /** 音量键翻页 */
    private boolean mIsVolumeEnabled;
    /** 主题 */
    private int mTheme;
    /** 是否是夜间模式 */
    private boolean mIsNightMode;
    /** 屏幕亮度 */
    private int mLight;
    private Context mContext;
    /** 左边缘的距离 */
    private int mMarginLeft;
    /** 右边缘的距离 */
    private int mMarginRight;
    /** 上边缘的距离 */
    private int mMarginTop = 22;
    /** 正文首行距顶部标题距离 */
    private int mMarginTopToTitle = 24;
    /** 下边缘的距离 */
    private int mMarginBottom = 22;
    /** 边宽度(单位dip) */
    public int mSideWidth = 10;
    /** 设备density */
    private float mDensity;
    /** 屏幕宽度 */
    private int mScreenWidth;
    /** 屏幕高度 */
    private int mScreenHeight;
    /** 顶部文字大小 */
    private int mTopTextSize = 30;
    /** 最大行间距 */
    public static final int MAX_LINESPACE = 28;// px
    /** 最小行间距 */
    public static final int MIN_LINESPACE = 10;// px
    /** 小说页背面颜色值索引 */
    private int mReadBackBg = 0xFFd3c3a9;
    /** 默认字体颜色 */
    private int mTextColor = 0xFF5B4C3B;
    /** 背景颜色 */
    private int mDefaultBackgroundColor = 0XFFD7C3A2;
    private int[] mBackGroundColor = {0XFFFAF7EF, 0XFFD7C3A2, 0XFFDBEFDA, 0XFF87A7B2, 0XFF879AB2,
            0XFFF2E0CD, 0XFFB8929C, 0XFF3D3F41, 0xFF2c2c2c};
    /** 通知栏高度 */
    private int mStatusBarHeight;
    /** 阅读页翻页模式 */
    private int mPageTurnMode = 0;
    private static BookContentSettings mSettings;

    /** 默认底部时间，百分比文字大小 */
    private static final int DEFAULT_BOTTOM_TEXTSIZE = 13;

    /** 是否是复制模式 */
    private boolean isCopyMode;
    /** 屏幕方向 (0：竖屏 1：横屏) */
    public static int screenDirection;
    /** 默认屏幕方向 (0：竖屏 1：横屏) */
    private static int defaultScreenDirection = 0;
    /** 电池高度 */
    private int mBatteryHeight;

    public static BookContentSettings getInstance(Context context) {
        if (mSettings == null) {
            mSettings = new BookContentSettings(context);
        }
        return mSettings;
    }

    public static void onDestroyInstance() {
        if (mSettings != null) {
            mSettings = null;
        }
    }

    private BookContentSettings(Context context) {
        mContext = context;
    }

    /**
     * 初始化 上下左右边缘的距离， 屏幕的高度 | 宽度 | 行间距 | 通知栏的高度 | 顶部文字大小 | 字体大小 | 屏幕亮度 | 显示主题 | 根据像素密度求出书边的宽度 signed
     * by youyang
     * 
     * @param density 像素密度
     */
    public void init(float density) {
        mDensity = density;
        SharedPreferenceUtil.getInstanceSharedPreferenceUtil().getShaPreferencesInstance(mContext);
        Resources res = mContext.getResources();
        mBatteryHeight = res.getDimensionPixelSize(R.dimen.page_battery_border_height);
        // 以下字体，风格...===========by lyz
        // 字档
        mSizePosition = SharedPreferenceUtil.getInstanceSharedPreferenceUtil().getSizePosition();

        // 风格
        mStyle = SharedPreferenceUtil.getInstanceSharedPreferenceUtil().getStyle();

        // 正文字号
        BOOKCONTENT_DEFAULT_TEXT_SIZE =
                res.getDimensionPixelSize(R.dimen.bookcontent_default_text_size);
        BOOKCONTENT_TEXT_SIZE_MAX = res.getDimensionPixelSize(R.dimen.bookcontent_text_size_max);
        BOOKCONTENT_TEXT_SIZE_MIN = res.getDimensionPixelSize(R.dimen.bookcontent_text_size_min);
        mTextSize = SharedPreferenceUtil.getInstanceSharedPreferenceUtil().getBookContentFontSize();

        // 标题字号
        TITLE_DEFAULT_TEXT_SIZE = res.getDimensionPixelSize(R.dimen.title_default_text_size);
        TITLE_TEXT_SIZE_CHANGE = res.getDimensionPixelSize(R.dimen.title_text_size_change);
        TITLE_TEXT_SIZE_MIN = res.getDimensionPixelSize(R.dimen.title_text_size_min);
        mTitleTextSize =
                SharedPreferenceUtil.getInstanceSharedPreferenceUtil()
                        .getBookContentTitleFontSize();
        // 标题与正文间距
        TITLE_DEFAULT_SPACE = res.getDimensionPixelSize(R.dimen.title_default_space);
        TITLE_SPACE_CHANGE = res.getDimensionPixelSize(R.dimen.title_space_change);
        TITLE_SPACES_MIN[0] = res.getDimensionPixelSize(R.dimen.title_space_min_default);
        TITLE_SPACES_MIN[1] = res.getDimensionPixelSize(R.dimen.title_space_min_one);
        TITLE_SPACES_MIN[2] = res.getDimensionPixelSize(R.dimen.title_space_min_two);
        TITLE_SPACES_MIN[3] = res.getDimensionPixelSize(R.dimen.title_space_min_three);
        mTitleSpace = SharedPreferenceUtil.getInstanceSharedPreferenceUtil().getTitleSpace();

        // 行间距
        LINE_DEFAULT_SPACE = res.getDimensionPixelSize(R.dimen.line_default_space);
        LINE_SPACE_CHANGE = res.getDimensionPixelSize(R.dimen.line_space_change);
        LINE_SPACES_MIN[0] = res.getDimensionPixelSize(R.dimen.line_space_min_default);
        LINE_SPACES_MIN[1] = res.getDimensionPixelSize(R.dimen.line_space_min_one);
        LINE_SPACES_MIN[2] = res.getDimensionPixelSize(R.dimen.line_space_min_two);
        LINE_SPACES_MIN[3] = res.getDimensionPixelSize(R.dimen.line_space_min_three);
        mLineSpace =
                SharedPreferenceUtil.getInstanceSharedPreferenceUtil().getBookContentLineSpace();

        // 段间距
        PARAGRAPH_DEFAULT_SPACE = res.getDimensionPixelSize(R.dimen.paragraph_default_space);
        PARAGRAPH_DEFAULT_CHANGE = res.getDimensionPixelSize(R.dimen.paragraph_space_change);
        PARAGRAPH_SPACES_MIN[0] = res.getDimensionPixelSize(R.dimen.paragraph_space_min_default);
        PARAGRAPH_SPACES_MIN[1] = res.getDimensionPixelSize(R.dimen.paragraph_space_min_one);
        PARAGRAPH_SPACES_MIN[2] = res.getDimensionPixelSize(R.dimen.paragraph_space_min_two);
        PARAGRAPH_SPACES_MIN[3] = res.getDimensionPixelSize(R.dimen.paragraph_space_min_three);
        mParagraphSpace =
                SharedPreferenceUtil.getInstanceSharedPreferenceUtil()
                        .getBookContentParagraphSpace();

        Log4an.e("liyizhe", "first   字档=" + mSizePosition + "，风格=" + mStyle + ",字号=" + mTextSize
                + ",标题字号=" + mTitleTextSize + ",标题距离=" + mTitleSpace + ",行间距=" + mLineSpace
                + ",段间距=" + mParagraphSpace);

        // 以上字体，风格...=============================
        // mScreenWidth = Util.getScreenWidth();
        // mScreenHeight = Util.getScreenHeight();

        if (SharedPreferenceUtil.getInstanceSharedPreferenceUtil().getBookContentPortraitScreen()) {
            mScreenWidth = Util.getScreenWidth();
            mScreenHeight = Util.getScreenHeight();
        } else {
            mScreenHeight = Util.getScreenWidth();
            mScreenWidth = Util.getScreenHeight();
        }

        mStatusBarHeight = Util.getStatusBarHeight();
        if (Constant.ISSHOWBOOKSIDE) {
            // 根据像素密度求出书边的宽度
            mSideWidth = (int) (mDensity * mSideWidth);
        } else {
            mSideWidth = 0;
        }

        mMarginLeft = res.getDimensionPixelSize(R.dimen.page_padding_left);
        mMarginRight = res.getDimensionPixelSize(R.dimen.page_padding_right);
        mTopTextSize = res.getDimensionPixelSize(R.dimen.page_text_size);
        mMarginTop =
                Util.getFontHeight(mTopTextSize)
                        + res.getDimensionPixelSize(R.dimen.page_text_margin_top);
        mMarginBottom =
                res.getDimensionPixelSize(R.dimen.page_padding_bottom)
                        + Util.getFontHeight(res.getDimensionPixelSize(R.dimen.page_text_size));
        mMarginTopToTitle = res.getDimensionPixelSize(R.dimen.page_text_margin_top_1);
        mLight = SharedPreferenceUtil.getInstanceSharedPreferenceUtil().getBookContentLight();
        mIsFullScreen =
                SharedPreferenceUtil.getInstanceSharedPreferenceUtil().getBookContentFullScreen();
        mIsVertialScreen =
                SharedPreferenceUtil.getInstanceSharedPreferenceUtil()
                        .getBookContentPortraitScreen();
        // setFullScreen();
        mIsVolumeEnabled =
                SharedPreferenceUtil.getInstanceSharedPreferenceUtil()
                        .getBookContentVolumeEnabled();
        mIsNightMode =
                SharedPreferenceUtil.getInstanceSharedPreferenceUtil().getBookContentNightMode();
        mTheme = PayTheme.getCurrentTheme(mContext);

        mPageTurnMode =
                SharedPreferenceUtil.getInstanceSharedPreferenceUtil().getBookContentPageTurnMode();
    }

    public void setVertialScreen(boolean isv) {
        mIsVertialScreen = isv;
    }

    public boolean getVertialScreen() {
        return mIsVertialScreen;
    }

    public void changeScreenWidthHeight() {
        int tempSize = 0;
        tempSize = mScreenHeight;
        mScreenHeight = mScreenWidth;
        mScreenWidth = tempSize;
    }

    // ==============以下风格字档对应逻辑===================by lyz
    /**
     * （变字档，风格不变） 改变字体大小：改变字档，字体大小，标题字号，标题距手段距离，行间距，段间距 须知道字档，（风格），获取对应行列式
     * 
     * @param textSize:要变成的字体大小
     * @param sizePositionChange：字档的改变值（1，-1）
     */
    public void setTextSize(int textSize, int sizePositionChange) {
        if (onSettingListener.getReadState() != BookContentModel.READ_STATE_NORMAL) {
            return;
        }

        if (this.mTextSize != textSize && textSize >= BookContentSettings.BOOKCONTENT_TEXT_SIZE_MIN
                && textSize <= BookContentSettings.BOOKCONTENT_TEXT_SIZE_MAX) {
            // 改变字档
            this.mSizePosition = mSizePosition + sizePositionChange;
            SharedPreferenceUtil.getInstanceSharedPreferenceUtil().saveSizePosition(mSizePosition);

            // 改变字体
            this.mTextSize = textSize;
            SharedPreferenceUtil.getInstanceSharedPreferenceUtil()
                    .saveBookContentFontSize(textSize);

            // 改变标题字体,改变前设置并保存好字档
            changeTitleTextSize();

            // 改变标题距段首距离,改变前设置字档,风格取全局变量没变
            changeTitleSpace();

            changeLineSpace();

            changeParagraphSpace();

            Log4an.e("liyizhe", "size 字档=" + mSizePosition + "，风格=" + mStyle + ",字号=" + mTextSize
                    + ",标题字号=" + mTitleTextSize + ",标题距离=" + mTitleSpace + ",行间距=" + mLineSpace
                    + ",段间距=" + mParagraphSpace);
            if (onSettingListener != null) {
                onSettingListener.onNotifyUpdateSettingsChanged();
            }
        }
    }

    /**
     * （变风格，字档不变） 改变风格：改变标题距手段距离，改变行间距，改变段间距 须知道（字档），风格，获取对应行列式
     * 
     * @param styleIndex：要改变成的风格索引（0,1,2,3）
     */
    public void setStyle(int styleIndex) {
        if (onSettingListener.getReadState() != BookContentModel.READ_STATE_NORMAL) {
            return;
        }

        // 保存要设置的风格
        this.mStyle = styleIndex;
        SharedPreferenceUtil.getInstanceSharedPreferenceUtil().saveStyle(mStyle);

        // 改变标题距段首距离,改变前设置风格,字档取全局变量没变
        changeTitleSpace();

        changeLineSpace();

        changeParagraphSpace();

        Log4an.e("liyizhe", "style 字档=" + mSizePosition + "，风格=" + mStyle + ",字号=" + mTextSize
                + ",标题字号=" + mTitleTextSize + ",标题距离=" + mTitleSpace + ",行间距=" + mLineSpace
                + ",段间距=" + mParagraphSpace);
        if (onSettingListener != null) {
            onSettingListener.onNotifyUpdateSettingsChanged();
        }
    }

    /**
     * change标题字体 ：根据字档单一决定
     */
    private void changeTitleTextSize() {
        this.mTitleTextSize = TITLE_TEXT_SIZE_MIN + mSizePosition * TITLE_TEXT_SIZE_CHANGE;
        SharedPreferenceUtil.getInstanceSharedPreferenceUtil().saveBookContentTitleFontSize(
                mTitleTextSize);
    }

    /**
     * change标题距段首距离 ：根据字档和风格二因素决定
     */
    private void changeTitleSpace() {
        // 取出对应风格的最小 标题距段首距离
        int titleSpaceMin = TITLE_SPACES_MIN[mStyle];
        // 改变后的距离为 最小的距离加上梯度乘以字档
        this.mTitleSpace = titleSpaceMin + mSizePosition * TITLE_SPACE_CHANGE;
        SharedPreferenceUtil.getInstanceSharedPreferenceUtil().saveTitleSpace(mTitleSpace);
    }

    /**
     * change行间距：根据字档和风格二因素决定
     */
    private void changeLineSpace() {
        // 取出对应风格的最小 行间距
        int lineSpaceMin = LINE_SPACES_MIN[mStyle];
        // 改变后的行间距为 最小的行间距加上梯度乘以字档
        this.mLineSpace = lineSpaceMin + mSizePosition * LINE_SPACE_CHANGE;
        SharedPreferenceUtil.getInstanceSharedPreferenceUtil().saveBookContentLineSpace(mLineSpace);
    }

    /**
     * change段间距：根据字档和风格二因素决定
     */
    private void changeParagraphSpace() {
        // 取出对应风格的最小 段间距
        int paragraphSpaceMin = PARAGRAPH_SPACES_MIN[mStyle];
        // 改变后的段间距为 最小的段间距加上梯度乘以字档
        this.mParagraphSpace = paragraphSpaceMin + mSizePosition * PARAGRAPH_DEFAULT_CHANGE;
        SharedPreferenceUtil.getInstanceSharedPreferenceUtil().saveBookContentParagraphSpace(
                mParagraphSpace);
    }

    // ==================以上风格字档对应逻辑=========================

    /**
     * 改变夜间模式
     * 
     * @param isUpdate
     * @param toLight 改变主题时传true;其他情况传false
     */
    public void changeNightMode(boolean isUpdate, boolean toLight) {
        mIsNightMode = !mIsNightMode;
        if (toLight) {
            mIsNightMode = false;
        }
        resetBuyButtonResId((mIsNightMode ? Constant.ORDER_BUY_BUTTON_ID[0]
                : Constant.ORDER_BUY_BUTTON_ID[1]));
        SharedPreferenceUtil.getInstanceSharedPreferenceUtil().saveBookContentNightMode(
                mIsNightMode);
        if (isUpdate) {
            setBackgroundIndex(mIsNightMode ? -1 : mTheme);
        }
    }

    /** 设置哪个背景 */
    public void setBackgroundIndex(int index) {
        if (index == -1) { // 夜间模式
            this.mTheme = PayTheme.PAY_THEME;
            SharedPreferenceUtil.getInstanceSharedPreferenceUtil().saveBookContentLastTheme(
                    SharedPreferenceUtil.getInstanceSharedPreferenceUtil().getBookContentTheme());

            SharedPreferenceUtil.getInstanceSharedPreferenceUtil().saveBookContentTheme(
                    PayTheme.PAY_THEME);

            // 设置亮度
            // setLight(mLight);
            setBackgroundTheme(PayTheme.PAY_THEME);
        } else {
            // 设置亮度
            // setLight(mLight);

            int tempTheme = mTheme;
            if (mTheme == PayTheme.PAY_THEME) {// 夜间模式下，先保存日间的主题,避免切换到日间时，主题复位到默认模式
                int lastTheme =
                        SharedPreferenceUtil.getInstanceSharedPreferenceUtil()
                                .getBookContentLastTheme();

                SharedPreferenceUtil.getInstanceSharedPreferenceUtil().saveBookContentTheme(
                        lastTheme);
                tempTheme = lastTheme;
                this.mTheme = lastTheme;
            } else {
                SharedPreferenceUtil.getInstanceSharedPreferenceUtil().saveBookContentTheme(mTheme);
            }
            setBackgroundTheme(tempTheme);
        }
    }

    /** 设置屏幕亮度 */
    public void setLight(int lightOnNight) {
        mLight = lightOnNight;
        Log4an.e("1", "lightOnNight=" + lightOnNight);
        if (onSettingListener != null) {
            onSettingListener.onSetLight(lightOnNight);
        }
    }

    /**
     * 设置系统亮度
     */
    public void setSystemLight() {
        if (onSettingListener != null) {
            onSettingListener.onSetSystemLight();
        }
    }

    public int getScreenDirection() {
        return screenDirection;
    }

    private OnSettingListener onSettingListener;

    public void setOnSettingListener(OnSettingListener onSettingListener) {
        this.onSettingListener = onSettingListener;
    }

    public interface OnSettingListener {
        /***
         * 设置亮度
         * 
         * @param light
         * @param isChangeSystem
         * @param mLight
         */
        public void onSetLight(int light);

        /**
         * 设置系统亮度
         */
        public void onSetSystemLight();

        /**
         * 设置参数改变，通知更新UI
         */
        public void onNotifyUpdateSettingsChanged();

        /**
         * 主题改变，通知更新UI
         * 
         * @param id
         */
        public void onNotifyUpdateTheme(int id);

        /**
         * 拿到当前阅读状态
         */
        public int getReadState();
    }


    public int getmLight() {
        return mLight;
    }

    /** 改变是音量键是否可用 */
    public void changeVolumeEnabled() {
        mIsVolumeEnabled = !mIsVolumeEnabled;
    }

    public boolean isFullScreen() {
        return mIsFullScreen;
    }

    public void setFullScreen(boolean isFullScreen) {
        this.mIsFullScreen = isFullScreen;
        if (isFullScreen) {
            mStatusBarHeight = 0;
        } else {
            mStatusBarHeight = Util.getStatusBarHeight();
        }
    }

    /** 改变全屏模式 */
    @Deprecated
    protected void changeFullScreen() {
        mIsFullScreen = !mIsFullScreen;
        SharedPreferenceUtil.getInstanceSharedPreferenceUtil().saveBookContentFullScreen(
                mIsFullScreen);
        if (mIsFullScreen) {
            ((Activity) mContext).getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        } else {
            // setFullScreen();
        }
        if (onSettingListener != null) {
            onSettingListener.onNotifyUpdateSettingsChanged();
        }
    }

    /**
     * @return the sideWidth
     */
    public int getSideWidth() {
        return mSideWidth;
    }

    /**
     * @param sideWidth the sideWidth to set
     */
    public void setSideWidth(int sideWidth) {
        this.mSideWidth = sideWidth;
    }

    /**
     * @return the density
     */
    public float getDensity() {
        return mDensity;
    }

    /**
     * @param density the density to set
     */
    public void setDensity(float density) {
        this.mDensity = density;
    }

    /**
     * @return the marginLeft
     */
    public int getMarginLeft() {
        return mMarginLeft;
    }

    /**
     * @param marginLeft the marginLeft to set
     */
    public void setMarginLeft(int marginLeft) {
        this.mMarginLeft = marginLeft;
    }

    /**
     * @return the marginRight
     */
    public int getMarginRight() {
        return mMarginRight;
    }

    /**
     * @param marginRight the marginRight to set
     */
    public void setMarginRight(int marginRight) {
        this.mMarginRight = marginRight;
    }

    public void setMarginBottom(int marginBottom) {
        this.mMarginBottom = marginBottom;
    }

    /**
     * @return the screenWidth
     */
    public int getScreenWidth() {
        return mScreenWidth;
    }

    /**
     * @return the screenHeight
     */
    public int getScreenHeight() {
        return mScreenHeight;
    }

    /**
     * @return the lineSpace
     */
    public float getLineSpace() {
        return mLineSpace;
    }

    /**
     * @param lineSpace the lineSpace to set
     */
    public void setLineSpace(int lineSpace) {
        if (this.mLineSpace != lineSpace && lineSpace >= MIN_LINESPACE
                && lineSpace <= MAX_LINESPACE) {
            this.mLineSpace = lineSpace;
            SharedPreferenceUtil.getInstanceSharedPreferenceUtil().saveBookContentLineSpace(
                    lineSpace);
            if (onSettingListener != null) {
                onSettingListener.onNotifyUpdateSettingsChanged();
            }
        }
    }

    /**
     * @return the marginTop
     */
    public int getMarginTop() {
        return mMarginTop;
    }

    /**
     * @return the marginBottom
     */
    public int getMarginBottom() {
        return mMarginBottom;
    }


    /**
     * @return the readBackBgIndex
     */
    public int getReadBackBgIndex() {
        return mReadBackBg;
    }

    /**
     * @param readBackBgIndex the readBackBgIndex to set
     */
    public void setReadBackBgIndex(int readBackBgIndex) {
        this.mReadBackBg = readBackBgIndex;
    }

    /**
     * @return the textColor
     */
    public int getTextColor() {
        return mTextColor;
    }

    /**
     * @param textColor the textColor to set
     */
    public void setTextColor(int textColor) {
        this.mTextColor = textColor;
    }

    public void setDefaultBackgroundColor(int color) {
        mDefaultBackgroundColor = color;
    }

    /**
     * @return the defaultBackgroundColor
     */
    public int getDefaultBackgroundColor() {
        return mDefaultBackgroundColor;
    }

    @Override
    public int getBackGroundColorFromTheme(int themeid) {// TODO
        if (themeid >= 0 && themeid <= 8) {
            return mBackGroundColor[themeid];
        } else
            return mDefaultBackgroundColor;
    }



    /**
     * @param mStatusBarHeight the mStatusBarHeight to set
     */
    public void setmStatusBarHeight(int mStatusBarHeight) {
        this.mStatusBarHeight = mStatusBarHeight;
    }

    /**
     * @return the statusBarHeight
     */
    public int getStatusBarHeight() {
        return mStatusBarHeight;
    }

    /**
     * @return the topTextSize
     */
    public int getTopTextSize() {
        return mTopTextSize;
    }

    public boolean isVolumeEnabled() {
        return mIsVolumeEnabled;
    }

    public boolean isNightMode() {
        return mIsNightMode;
    }

    public int getTheme() {
        return mTheme;
    }

    public void setTheme(int theme) {
        this.mTheme = theme;
        setBackgroundTheme(theme);
    }



    public int getTopToTitle() {
        return mMarginTopToTitle;
    }

    public void setMarginTopToTitle(int marginTopToTitle) {
        this.mMarginTopToTitle = marginTopToTitle;
    }



    public int getmPageTurnMode() {
        return mPageTurnMode;
    }

    public void setmPageTurnMode(int mPageTurnMode) {
        this.mPageTurnMode = mPageTurnMode;
    }

    /**
     * @return the mBatteryHeight
     */
    public int getmBatteryHeight() {
        return mBatteryHeight;
    }

    /**
     * @param mBatteryHeight the mBatteryHeight to set
     */
    public void setmBatteryHeight(int mBatteryHeight) {
        this.mBatteryHeight = mBatteryHeight;
    }

    // ===================================
    public int getmSizePosition() {
        return mSizePosition;
    }

    public void setmSizePosition(int position) {
        this.mSizePosition = position;
    }

    public int getmStyle() {
        return mStyle;
    }

    public void setmStyle(int style) {
        this.mStyle = style;
    }

    /**
     * @return the textSize
     */
    public int getTextSize() {
        return mTextSize;
    }

    public int getmTitleTextSize() {
        return mTitleTextSize;
    }

    public void setmTitleTextSize(int mTitleTextSize) {
        this.mTitleTextSize = mTitleTextSize;
    }

    public int getmTitleSpace() {
        return mTitleSpace;
    }

    public void setmTitleSpace(int mTitleSpace) {
        this.mTitleSpace = mTitleSpace;
    }

    public int getmLineSpace() {
        return mLineSpace;
    }

    public void setmLineSpace(int mLineSpace) {
        this.mLineSpace = mLineSpace;
    }

    public void setmParagraphSpace(int mParagraphSpace) {
        this.mParagraphSpace = mParagraphSpace;
    }

    public int getmParagraphSpace() {
        return mParagraphSpace;
    }

    // ====================================

    /**
     * 设置背景主题
     * 
     * @param index
     */
    private void setBackgroundTheme(int index) {
        if (onSettingListener != null) {
            onSettingListener.onNotifyUpdateTheme(index);
        }
    }


    public int getKeepScreenTime() {
        return SharedPreferenceUtil.getInstanceSharedPreferenceUtil()
                .getBookContentKeepScreenTime();
    }


    /**
     * 获取屏幕可绘制几行文字
     * 
     * @return
     */
    public int getScreenVisibleLineNum() {
        int lineNum;
        final int textHeight = getTextSize();

        final int visibleHeight =
                getScreenHeight() - getMarginTop() - getMarginBottom() - 2 * getTopToTitle();
        lineNum = (int) (visibleHeight / (textHeight + getLineSpace()));

        float surplusHeight = visibleHeight % (textHeight + getLineSpace());

        // Log.d(TAG, "surplusHeight : " + surplusHeight + " textHeight : " + textHeight);

        if (surplusHeight > textHeight) {
            lineNum++;
            surplusHeight -= textHeight;
        }

        // float addLineSpace = (surplusHeight / lineNum - 1);

        // setLineSpace(DEFAULT_LINESPACE + addLineSpace);

        // Log.d(TAG, "getScreenVisibleLineNum addLineSpace ： " + addLineSpace);

        return lineNum;
    }

    public Context getContext() {
        return mContext;
    }

    /**
     * 获取字体样式
     * 
     * @return
     */
    public Typeface getTypeface() {
        return Typeface.DEFAULT;
    }

    /**
     * 获取底部字体颜色
     * 
     * @remark 电池颜色，时间，百分比，顶部标题颜色
     * @return
     */
    public int getTopAndBottomTextColor() {
        return Constant.READ_OTHER_COLORS[PayTheme.getCurrentTheme(mContext)];
    }

    /**
     * 获取底部文字大小
     * 
     * @return
     */
    public int getBottomTextSize() {
        return DEFAULT_BOTTOM_TEXTSIZE * (int) mDensity;
    }

    /**
     * 获取订单页按钮文字大小
     * 
     * @return
     */
    public int getButtonTextSize() {
        return (int) (16 * mDensity);
    }

    /**
     * 获取订单页提示语文字颜色
     * 
     * @return
     */
    public int getTipTextColor() {
        return Constant.READ_TOAST_COLORS[PayTheme.getCurrentTheme(mContext)];
    }

    /**
     * 获取订单文字颜色
     * 
     * @return
     */
    public int getPayTitleTextColor() {
        return Constant.READ_TITLE_COLORS[PayTheme.getCurrentTheme(mContext)];
    }

    /**
     * 获取订单标题文字大小
     * 
     * @return
     */
    public int getPayTitleTextSize() {
        return (int) (21 * mDensity);
    }

    public void resetBuyButtonResId(int resId) {
        this.resId = resId;
    }

    private int resId;// 订单页购买按钮ID

    @Override
    public int resetBuyButtonResId() {
        return resId;
    }

    @Override
    public int getBuyButtonTextColor() {
        return Constant.READ_BUYBUTTON_TEXT_COLORS[PayTheme.getCurrentTheme(mContext)];
    }

    public boolean isCopyMode() {
        return isCopyMode;
    }

    /** 设置是否是复制模式 */
    public void setCopyMode(boolean isCopyMode) {
        if (this.isCopyMode == !isCopyMode) {
            this.isCopyMode = isCopyMode;
        }
    }

}
